import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;

import java.util.Arrays;

/**
 * @author xubobo
 * @date 2021/1/29
 **/
@Aspect
public class DemoAspect {
    @Around(value = "@annotation(DemoAnnotation) && execution(* *(..))")
    public Object aroundJoinPoint(ProceedingJoinPoint joinPoint) throws Throwable {
        return exec(joinPoint);
    }

    private Object exec(ProceedingJoinPoint joinPoint) {
        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
        String className = methodSignature.getDeclaringType().getSimpleName();
        String methodName = methodSignature.getName();
        Object[] args = joinPoint.getArgs();
        String callee = className + "." + methodName;

        Object result = null;
        try {
            System.out.println(joinPoint);
            System.out.println(callee + "方法执行前，方法参数：" + Arrays.toString(args));
            result = joinPoint.proceed();
            System.out.println(callee + "方法执行正常");
        } catch (Throwable t) {
            System.out.println(callee + "方法执行异常");
            t.printStackTrace();
        } finally {
            System.out.println(callee + "方法执行后");
        }
        return result;
    }
}
